package easy;

/**
 * 1221. 分割平衡字符串
 */
public class balancedStringSplit {

    public static void main(String[] args) {
        String s = "RLRRLLRLRL";
        System.out.println(myCode2(s));
    }

    public static int myCode(String s) {

        int ans = 0;
        int R = 0;
        int L = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'R') {
                R++;
            } else {
                L++;
            }

            if (R == L) {
                ans++;
//                不用置0
//                R = 0;
//                L = 0;
            }
        }
        return ans;
    }

    public static int myCode2(String s) {

        int ans = 0;
        int num = 0;

        for (char c : s.toCharArray()) {
            if (c == 'R') {
                num++;
            } else {
                num--;
            }
            if (num == 0) {
                ans++;
            }
        }
        return ans;
    }
}
